package com.tvunetworks.center.user.util;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;

/**
 * @program: tvucc-aggregator
 * @description: http请求相关工具方法
 * @author Kevin_Li
 * @create: 2019-10-10 10:33
 **/
public class HttpRequestUtil {
    /**
     *
     * @param request request
     * @return String
     * @throws Exception 编译
     */
    public static String getRequestDate(HttpServletRequest request) throws Exception {
        String line;
        StringBuilder buffer = new StringBuilder();
        BufferedReader reader = request.getReader();
        BufferedReader bufferReader = new BufferedReader(reader);
        while ((line = bufferReader.readLine()) != null) {
            buffer.append(line);
        }
        return buffer.toString();
    }

    /**
     *
     * @param request request
     * @return String
     */
    public static String getIpAddress(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }
}
